//首页模块相关的数据管理
import {RECEIVE_ADDRESS, RECEIVE_CATEGORYS, RECEIVE_SHOPS, RECEIVE_USER} from "../mutation-types";
import {
    reqAddress,
    reqFoodCategorys,
    reqShops,
} from '@/api/index.js'

export default {
    state: {
        latitude: 23.039389,
        longitude: 113.394105,
        address: {},
        category: [],
        shops: [],
    },
    mutations: {
        [RECEIVE_ADDRESS](state, address) {
            state.address = address
        },
        [RECEIVE_CATEGORYS](state, category) {
            state.category = category
        },
        [RECEIVE_SHOPS](state, shops) {
            state.shops = shops
        },
        [RECEIVE_USER](state, user) {
            state.userInfo = user
        }
    },
    actions: {
        //获取当前地址信息的异步action
        async getAddress({commit, state}) {
            const {longitude, latitude} = state
            //发异步请求
            const result = await reqAddress(longitude, latitude)
            //请求成功，提交给mutation
            if (result.code === 0) {
                const address = result.data
                commit(RECEIVE_ADDRESS, address)
            }
        },
        async getFoodCategory({commit, state}) {
            //发异步请求
            const result = await reqFoodCategorys()
            //请求成功，提交给mutation
            if (result.code === 0) {
                const category = result.data
                commit(RECEIVE_CATEGORYS, category)
            }
        },
        //获取商家列表
        async getShops({commit, state}) {
            const {longitude, latitude} = state
            //发异步请求
            const result = await reqShops({longitude, latitude})
            //请求成功，提交给mutation
            if (result.code === 0) {
                const shops = result.data
                commit(RECEIVE_SHOPS, shops)
            }
        },
        getUser({commit}, userInfo) {
            commit(RECEIVE_USER, userInfo)
        },
    },
    getters: {}
}